/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dbdir.core.xml;

import java.util.List;

/**
 *
 * @author thotheolh
 */
public class XMLMaker { //@comment Makes the XML file for JCurrency config and cache datastore

    private List parameterList;
    private String XML_VERSION = "1.0";
    private String ENCODING = "UTF-8";
    private StringBuffer finalDocument;

    public XMLMaker() {
        init();
    }

    public String writeXMLHead() { //beginning of XML document
        return "<?xml version=\"" + this.getXML_VERSION() + "\" encoding=\"" + this.getENCODING() + "\"?>";
    }

    public StringBuffer writeElement(Element element) { //Element only
        StringBuffer strBuff = new StringBuffer();
        strBuff.append("<" + element.getKey() + ">");
        strBuff.append(element.getValue());
        strBuff.append("</" + element.getKey() + ">");
        return strBuff;
    }

    public StringBuffer writeElementWithParam(Element element, Parameter param) { //Element with single parameter
        StringBuffer strBuff = new StringBuffer();
        strBuff.append("<" + element.getKey() + " " + param.getKey() + "=\"" + param.getValue() + "\">");
        strBuff.append(element.getValue());
        strBuff.append("</" + element.getKey() + ">");
        return strBuff;
    }

    public StringBuffer writeElementWithParam(Element element, List parameterList) { //Element with multiple parameters
        StringBuffer strBuff = new StringBuffer();
        strBuff.append("<" + element.getKey() + ">");
        //TODO: Loop through parameter list and add parameter
        strBuff.append(element.getValue());
        strBuff.append("</" + element.getKey() + ">");
        return strBuff;
    }

    public StringBuffer writeStandaloneElement(Element element) { //Standalone Element e.g. <xml/>
        StringBuffer strBuff = new StringBuffer();
        strBuff.append("<" + element.getKey() + "/>");
        return strBuff;
    }

    public StringBuffer writeStandaloneWithParam(Element element, List parameterList) { //Standalone Element with parameter(s) e.g. <element param="param"/>
        StringBuffer strBuff = new StringBuffer();
        Element listItem;
        strBuff.append("<" + element.getKey());
        for (int i = 0; i < parameterList.size(); i++) {
            listItem = (Element) parameterList.get(i);
            strBuff.append(" " + listItem.getKey() + "=\"" + listItem.getValue() + "\"");

        }
        strBuff.append("/>");
        return strBuff;
    }

    public StringBuffer openNestedElement(Element element) {
        StringBuffer strBuff = new StringBuffer();
        strBuff.append("<" + element.getKey() + ">");
        return strBuff;
    }

    public StringBuffer openNestedElementWithParam(Element element, Parameter param) {
        StringBuffer strBuff = new StringBuffer();
        strBuff.append("<" + element.getKey() + " " + param.getKey() + "=\"" + param.getValue() + "\">");
        return strBuff;
    }

    public StringBuffer openNestedElementWithParam(Element element, List parameterList) {
        StringBuffer strBuff = new StringBuffer();
        Element listItem;
        strBuff.append("<" + element.getKey());
        for (int i = 0; i < parameterList.size(); i++) {
            listItem = (Element) parameterList.get(i);
            strBuff.append(" " + listItem.getKey() + "=\"" + listItem.getValue() + "\"");

        }
        strBuff.append(">");
        return strBuff;
    }

    public StringBuffer closeNestedElement(String key) {
        StringBuffer strBuff = new StringBuffer();
        strBuff.append("</" + key + ">");
        return strBuff;
    }

    public StringBuffer writeComment(String comment) {
        StringBuffer strBuff = new StringBuffer();
        strBuff.append("<!-- ");
        strBuff.append(comment);
        strBuff.append(" -->");
        return strBuff;
    }

    public void doIndent() { // do indenting of text
    }

    public void doIndent(int indentSpace) {
    }

    public StringBuffer doNextLine() {
        StringBuffer strBuff = new StringBuffer();
        strBuff.append("/n/r");
        return strBuff;
    }

    public String nextLine() {
        return "/r/n";
    }

    /**
     * @return the XML_VERSION
     */
    public String getXML_VERSION() {
        return XML_VERSION;
    }

    /**
     * @param XML_VERSION the XML_VERSION to set
     */
    public void setXML_VERSION(String XML_VERSION) {
        this.XML_VERSION = XML_VERSION;
    }

    /**
     * @return the ENCODING
     */
    public String getENCODING() {
        return ENCODING;
    }

    /**
     * @param ENCODING the ENCODING to set
     */
    public void setENCODING(String ENCODING) {
        this.ENCODING = ENCODING;
    }

    public void init() {
        //TODO:Default init - set xml version and encoding, set root element
        this.writeXMLHead();
        Element rootElement = new Element();
//        rootElement.setKey("root");
//        this.writeElement(rootElement);
    }

    public void makeXML() {
        //TODO: Finally take the entire XML in the StringBuffer and dump it into a file
    }
}
